/*
 * Created on Wed Oct 11 2017
 * Copyright (c) 2017 jianglinjie
 */
'use strict';

var _captcha = require('../captcha');

var _captcha2 = _interopRequireDefault(_captcha);

var _TestConst = require('./TestConst');

var TestConst = _interopRequireWildcard(_TestConst);

var _CaptchaBusiness = require('../../constants/CaptchaBusiness');

var CaptchaBusiness = _interopRequireWildcard(_CaptchaBusiness);

var _auth = require('../auth');

var _auth2 = _interopRequireDefault(_auth);

var _StringUtils = require('../../utils/StringUtils');

var _serviceTypes = require('../../serviceTypes');

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

test('Account_Login', _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
  var token, curUser;
  return regeneratorRuntime.wrap(function _callee$(_context) {
    while (1) {
      switch (_context.prev = _context.next) {
        case 0:
          _context.next = 2;
          return _auth2.default.loginWithAccount(TestConst.USER_NAME, TestConst.USER_NAME_PWD);

        case 2:
          _context.next = 4;
          return _auth2.default.getToken();

        case 4:
          token = _context.sent;

          expect(token).toBeTruthy();

          _context.next = 8;
          return _auth2.default.getCurrentUserInfo();

        case 8:
          curUser = _context.sent;

          expect(curUser.uid).toBeTruthy();
          expect(curUser.userName).toMatch(TestConst.USER_NAME);
          expect(curUser.encryptPwd).toMatch((0, _StringUtils.md5)(TestConst.USER_NAME_PWD));

        case 12:
        case 'end':
          return _context.stop();
      }
    }
  }, _callee, undefined);
})));

test('Phone_Login', _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
  var captchaResult, token, curUser;
  return regeneratorRuntime.wrap(function _callee2$(_context2) {
    while (1) {
      switch (_context2.prev = _context2.next) {
        case 0:
          _context2.next = 2;
          return _captcha2.default.request({
            phone: TestConst.PHONE,
            business: CaptchaBusiness.LOGIN
          });

        case 2:
          captchaResult = _context2.sent;
          _context2.next = 5;
          return _auth2.default.loginWithPhoneCaptcha(TestConst.PHONE, captchaResult.captcha);

        case 5:
          _context2.next = 7;
          return _auth2.default.getToken();

        case 7:
          token = _context2.sent;

          expect(token).toBeTruthy();

          _context2.next = 11;
          return _auth2.default.getCurrentUserInfo();

        case 11:
          curUser = _context2.sent;

          expect(curUser.uid).toBeTruthy();
          expect(curUser.phone).toMatch(TestConst.PHONE);
          expect(curUser.encryptPwd).toMatch((0, _StringUtils.md5)(TestConst.PHONE_PWD));

        case 15:
        case 'end':
          return _context2.stop();
      }
    }
  }, _callee2, undefined);
})));

test('Auto_Login', _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
  var token, curUser;
  return regeneratorRuntime.wrap(function _callee3$(_context3) {
    while (1) {
      switch (_context3.prev = _context3.next) {
        case 0:
          _context3.next = 2;
          return _auth2.default.loginWithAccount(TestConst.USER_NAME, TestConst.USER_NAME_PWD);

        case 2:
          _context3.next = 4;
          return _auth2.default.autoLogin();

        case 4:
          _context3.next = 6;
          return _auth2.default.getToken();

        case 6:
          token = _context3.sent;

          expect(token).toBeTruthy();

          _context3.next = 10;
          return _auth2.default.getCurrentUserInfo();

        case 10:
          curUser = _context3.sent;

          expect(curUser.uid).toBeTruthy();
          expect(curUser.userName).toMatch(TestConst.USER_NAME);
          expect(curUser.encryptPwd).toMatch((0, _StringUtils.md5)(TestConst.USER_NAME_PWD));

        case 14:
        case 'end':
          return _context3.stop();
      }
    }
  }, _callee3, undefined);
})));

test('Logout', _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() {
  var token1, token2;
  return regeneratorRuntime.wrap(function _callee4$(_context4) {
    while (1) {
      switch (_context4.prev = _context4.next) {
        case 0:
          _context4.next = 2;
          return _auth2.default.loginWithAccount(TestConst.USER_NAME, TestConst.USER_NAME_PWD);

        case 2:
          _context4.next = 4;
          return _auth2.default.getToken();

        case 4:
          token1 = _context4.sent;

          expect(token1).toBeTruthy();

          _context4.next = 8;
          return _auth2.default.logout();

        case 8:
          _context4.next = 10;
          return _auth2.default.getToken();

        case 10:
          token2 = _context4.sent;

          expect(token2).toBeFalsy();

        case 12:
        case 'end':
          return _context4.stop();
      }
    }
  }, _callee4, undefined);
})));